package net.esj.basic.core.engine.util;

import java.util.Random;

public abstract class RandomUtils {
	
	private static Random _random = new Random();

	/**
	 * get random number from 0 to n-1
	 * @param n
	 * @return
	 */
	public static final int get(int n)
    {
        return (int) (_random.nextDouble() * n);
    }
    
	/**
	 * get random number from min to max (not max-1 !)
	 * @param min
	 * @param max
	 * @return
	 */
    public static final int get(int min, int max) 
    {
        return min + (int) Math.floor(_random.nextDouble() * (max - min + 1));
    }
    
    public static final int nextInt(int n)
    {
        return (int) Math.floor(_random.nextDouble() * n);
    }

    public static final int nextInt()
    {
        return _random.nextInt();
    }
    
    public static final double nextDouble()
    {
        return _random.nextDouble();
    }
    
    public static final double nextGaussian()
    {
        return _random.nextGaussian();
    }
    
    public static final boolean nextBoolean()
    {
        return _random.nextBoolean();
    }
    
    public static final void nextBytes(byte[] array)
    {
    	_random.nextBytes(array);
    }
    
    public static final int[] getRndUnrepeat(int min, int max, int mount){
    	int[] arr = new int[max-min+1];
    	int[] arr2 = new int[mount];
    	for(int i=min; i<=max; i++){
    		arr[i-min] = i;
    	}
    	for(int i=0; i<mount; i++){
    		int index=nextInt(arr.length-i-1);
    		arr2[i]=arr[index];
    	    arr[index]=arr[arr.length-i-1];
    	}
    	return arr2;
    }
    public static final int[] getRndUnrepeat(int min, int max, int mount, int set){
    	int[] arr = new int[max-min+1];
    	int[] arr2 = new int[mount];
    	for(int i=min; i<=max; i++){
    		if(set == i)
    			arr[i-min] = max;
    		else
    			arr[i-min] = i;
    	}
    	for(int i=0; i<mount; i++){
    		int index=nextInt(arr.length-i);
    		arr2[i]=arr[index];
    	    arr[index]=arr[arr.length-i];
    	}
    	return arr2;
    }
    
    /**
     * 获取一系列随机数
     * @param length 为该系列随机数的长度
     * @return
     */
    public static final String getRndNum(int length){
    	StringBuffer stringBuffer = new StringBuffer();
    	for(int i = 0 ; i < length ; i++){
    		stringBuffer.append(nextInt(10) + "");
    	}
    	return stringBuffer.toString();
    }
    /**
     * 获取一系列随机数字字母组合
     * @param length 为该系列随机数的长度
     * @return
     */
    public static final String getRanComplex(int length){
    	StringBuffer stringBuffer = new StringBuffer();
    	String s = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	for(int i = 0 ; i < length ; i++){
    		stringBuffer.append(s.charAt(nextInt(s.length())) + "");
    	}
    	return stringBuffer.toString();
    }
    
    /**
     * 获取一系列随机数字字母组合(字母大写，数字与字母出现概率一致)
     * @param length 为该系列随机数的长度
     * @return
     */
    public static final String getRanComplexUp(int length){
    	StringBuffer stringBuffer = new StringBuffer();
    	String s1 = "1234567890";
    	String s2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	for(int i = 0 ; i < length ; i++){
    		if(nextInt(10000)<5000){
    			stringBuffer.append(s1.charAt(nextInt(s1.length())) + "");
    		}else{
    			stringBuffer.append(s2.charAt(nextInt(s2.length())) + "");
    		}
    	}
    	return stringBuffer.toString();
    }
}
